﻿var qc_dau_count:Number=0;
var qc_cuoi_count:Number=0;
var qcc_count:Number=0;
function reset_video()
{
	_root.video_width_tween.rewind();
	_root.video_height_tween.rewind();		
	_root.video_x_tween.rewind();		
	_root.video_y_tween.rewind();				
	_root.small=false;
}
function qc_dau_func2(stream_ns_var:NetStream):Void
{
	_root.video_region.my_video._visible=false;
	qc_dau_count++;
	if(qc_dau_count>qcd_length*10)	
	{
		unloadMovie("image_mc");		
		clearInterval(time_id1);		
		_root.video_region.my_video._visible=true;		
		_root.video_region.my_video.attachVideo(stream_ns_var);
		stream_ns_var.play(_root.film_name);
		flv_mc.attachAudio(stream_ns_var);	
		qc_dau=false;
	   txt_ini_loading._visible=true;	
		time_id2 = setInterval(play_film, time_interval_ms, stream_ns);											
	}
}	
function qc_dau_func(stream_ns_var:NetStream,start_ad_stream_var:NetStream):Void
{
	qc_dau_count++;
	if((start_ad_stream_var.bytesLoaded==start_ad_stream_var.bytesTotal&&qcd_end==true)||(qc_dau_count>qcd_length*10))
	{
		clearInterval(time_id1);
		start_ad_stream_var.pause();
		_root.video_region.my_video.attachVideo(stream_ns_var);				
		_root.video_region.my_video.attachVideo(stream_ns_var);
		stream_ns_var.play(_root.film_name);
		flv_mc.attachAudio(stream_ns_var);	
		qc_dau=false;
//		test_ins.htmlText="choi phim day";
		time_id2 = setInterval(play_film, time_interval_ms, stream_ns);									
	}
}
var start_x:Number	= total_length._x ;
var end_x:Number	= total_length._x + total_length._width - thanh_truot1._width;
var running:Boolean=false;
var load_completed:Boolean=false;
function play_film(my_ns:NetStream):Void 
{
   if(my_ns.bytesLoaded<my_ns.bytesTotal&&my_ns.bytesTotal>10)
   {
	   pctLoaded= Math.round(my_ns.bytesLoaded / my_ns.bytesTotal * 100);
	   _root.loaded_bar._width=Math.round(_root.total_length._width*my_ns.bytesLoaded / my_ns.bytesTotal);
   }
   else if (_root.load_completed==false&&my_ns.bytesTotal>10)
   {
//	   trace("load complet" + my_ns.bytesTotal);
   	   _root.load_completed=true;
	   _root.loaded_bar._width = _root.total_length._width;
   }
	if (_root.is_pause == true) 
	{
		return;
	}	   		
	if (_root.slide_in_used == false) 
	{
		total_time = my_ns.time;		
		second = Math.round(total_time%60);
		second_string= String(Math.floor(second/10))+String((Math.round(second)%10));
		minute = Math.floor(total_time/60);
		minute_string= String(Math.floor(minute/10))+String((Math.round(minute)%10));
		_root.time_region.time_playing_str.htmlText = minute_string+":"+second_string;
	}

}

function qcc_call()
{
	clearInterval(_root.time_id2);			
	_root.played_tween.stop();
	_root.run_tween.stop();	
	if(qcc_type=="video")
	{
		_root.video_region.my_video.attachVideo(end_ad_stream);	
		flv_mc.attachAudio(end_ad_stream);	
		end_ad_stream.play(qcc_name);
		time_id3 = setInterval(qc_cuoi_func,100,end_ad_stream);					
		return;
	}	
	else 
	{
		_root.video_region.my_video._visible=false;
		load_quang_cao_cuoi(qcc_name);
	}	
	qc_cuoi=true;

}
function qc_cuoi_func2(end_ad_stream_var:NetStream):Void
{
	qc_cuoi_count++;	
	if(qc_cuoi_count>qcc_length*10)	
	{
		clearInterval(time_id3);
		unloadMovie("image_mc");		
		qc_cuoi=false;
		related_film_func();				
	}
}
function qc_cuoi_func(end_ad_stream_var:NetStream):Void
{
	qc_cuoi_count++;
	if((end_ad_stream_var.bytesLoaded==end_ad_stream_var.bytesTotal&&qcc_end==true)||(qc_cuoi_count>qcc_length*10))
	{
		clearInterval(time_id3);
		_root.video_region.my_video._visible=false;
		end_ad_stream_var.pause();
		qc_cuoi=false;
		related_film_func();	
		_root.video_region.my_video.attachVideo(_root.stream_ns);	
		_root.flv_mc.attachAudio(stream_ns);			
	}	
}		

var total_change:Number = 0;
function related_film_func() {

	my_full_mode 	= full_mode;
	id_arr 			= string_to_array(related_film_id, ',');
	name_arr 		= string_to_array(related_film_name, ',');
	display_arr 	= string_to_array(related_film_display, ',');
	length_arr 		= string_to_array(related_film_length, ',');
	author_arr 		= string_to_array(related_film_author, ',');
	image_arr 		= string_to_array(related_film_image, ',');
	star_score_arr	= string_to_array(related_film_star_score,',');	
	num_of_film = name_arr.length;
	if(num_of_film<=0||name_arr[0]==undefined)
	{
		replay_ins2._visible=true;
		return;
	}
	if (num_of_film>0) {
		load_related_film(my_related_movie0,0);
	}
	if (num_of_film>1) {
		duplicateMovieClip(my_related_movie0,"my_related_movie1",30);
		my_related_movie1._x=my_related_movie0._x;
		my_related_movie1._y=my_related_movie0._y+95;				
		_root.my_related_movie1.image_x = _root.my_related_movie1.hien_image_ins._x;
		_root.my_related_movie1.image_y = _root.my_related_movie1.hien_image_ins._y;		
		load_related_film(my_related_movie1,1);		
	}
	if (num_of_film>2) {
		duplicateMovieClip(my_related_movie0,"my_related_movie2",31);
		my_related_movie2._x=my_related_movie1._x;
		my_related_movie2._y=my_related_movie1._y+95;						
		_root.my_related_movie2.image_x = _root.my_related_movie2.hien_image_ins._x;
		_root.my_related_movie2.image_y = _root.my_related_movie2.hien_image_ins._y;		
		load_related_film(my_related_movie2,2);		
	}	
	if (num_of_film>2) 
	{	
		related_previous_ins._visible=true;
		related_next_ins._visible=true;		
		related_previous_ins._y=my_related_movie3._y + 100;		
		related_next_ins._y=related_previous_ins._y;				
	}
}
var film_star:Number=1;
function load_related_film(movie_name:MovieClip,movie_number:Number)
{
	_root.film_star = Math.round(2*star_score_arr[movie_number]);	
	related_movie_ins.draw_button(movie_name,movie_name.hien_image_ins,movie_name.star,display_arr[movie_number],length_arr[movie_number],_root.film_star,author_arr[movie_number],image_dir+image_arr[movie_number]);	
	_root.my_film_id = id_arr[movie_number];	
	movie_name.background_end.onRelease=function()
	{
		ExternalInterface.call("myFlash_DoFSCommand("+id_arr[movie_number]+")");
	}

}

my_cn.onStatus = function(infoObject)
{
	if(infoObject.code=="NetConnection.Call.Failed")
	{
		//trace("NetConnection.Call.Failed");
	}
	if(infoObject.code=="NetConnection.Connect.AppShutdown")
	{
		//trace("NetConnection.Connect.AppShutdown");
	}
	if(infoObject.code=="NetConnection.Connect.Closed")
	{
		//trace("NetConnection.Connect.Closed");
		//error_occurr=true;		
		//reconnect();
		gotoAndPlay(2);
	}
	if(infoObject.code=="NetConnection.Connect.Failed")
	{
		//trace("NetConnection.Connect.Failed");
		gotoAndPlay(2);		
	}
	if(infoObject.code=="NetConnection.Connect.InvalidApp")
	{
		//trace("NetConnection.Connect.InvalidApp");
	}
	if(infoObject.code=="NetConnection.Connect.Rejected")
	{
		//trace("NetConnection.Connect.Rejected");
	}
	if(infoObject.code=="NetConnection.Connect.Success")
	{
		//trace("NetConnection.Connect.Success");
		error_occurr=false;
	}

};
start_ad_stream.onStatus=function(infoObject1)
{
	if(infoObject1.code=="NetStream.Play.Stop")
	{
		//trace("NetStream.Play.Stop");
		_root.qcd_end=true;
	}			
}
end_ad_stream.onStatus=function(infoObject1)
{
	if(infoObject1.code=="NetStream.Play.Stop")
	{
		//trace("NetStream.Play.Stop");
		qcc_end=true;
	}			
}
var tween_started:Boolean=false;
stream_ns.onStatus=function(infoObject1)
{
	if(infoObject1.code=="NetStream.Buffer.Empty")
	{
		//trace("NetStream.Buffer.Empty");
		_root.buffer_full=false;
		if(qc_dau==true||qc_cuoi==true||end_film==true||_root.is_pause==true||_root.slide_in_used)
		return;		
		_root.played_tween.stop();
		_root.run_tween.stop();
	}
	if(infoObject1.code=="NetStream.Buffer.Full")
	{
		//trace("NetStream.Buffer.Full");
		if(qc_dau==true||qc_cuoi==true||end_film==true||_root.is_pause==true||_root.slide_in_used)
			return;		
		if(_root.tween_started==false)
		{
			_root.played_tween = new mx.transitions.Tween(played_ins, "_width", mx.transitions.easing.None.easeNone, start_x, end_x, video_length, true);			
			_root.run_tween = new mx.transitions.Tween(thanh_truot1, "_x", mx.transitions.easing.None.easeNone, start_x, end_x, video_length, true);			
			_root.tween_started=true;
		}
		else if(_root.is_pause==false)
		{
			played_tween.resume();
			run_tween.resume();
		}

		can_play_time += stream_ns.bufferLength;
		_root.buffer_full=true;
		txt_ini_loading._visible=false;
		seeking=false;
	}	
	if(infoObject1.code=="NetStream.Failed")
	{
		//trace("NetStream.Failed");
//		test1.htmlText="FAIL";
	}		
	if(infoObject1.code=="NetStream.Failed")
	{
		//trace("NetStream.Failed");
	}			
	
	if(infoObject1.code=="NetStream.Pause.Notify")
	{
		//trace("NetStream.Pause.Notify");
		//test1.htmlText="Pause notify";				
	}			
	
	if(infoObject1.code=="NetStream.Play.Complete")
	{
		//trace("NetStream.Play.Complete");
	}			
	
	if(infoObject1.code=="NetStream.Play.Failed")
	{
		//trace("NetStream.Play.Failed");
	}			
	
	if(infoObject1.code=="NetStream.Play.InsufficientBW")
	{
		//trace("NetStream.Play.InsufficientBW");
	}			
	
	if(infoObject1.code=="NetStream.Play.PublishNotify")
	{
		//trace("NetStream.Play.PublishNotify");
	}			
	
	if(infoObject1.code=="NetStream.Play.Reset")
	{
		//trace("NetStream.Play.Reset");
		//test1.htmlText="Film RESET";		
	}			
	
	if(infoObject1.code=="NetStream.Play.Start")
	{
		//trace("NetStream.Play.Start");
		//test1.htmlText="Film Start";
	}			
	
	if(infoObject1.code=="NetStream.Play.Stop")
	{				
		film_stop();		
	}			
	
	if(infoObject1.code=="NetStream.Play.StreamNotFound")
	{
		//trace("NetStream.Play.StreamNotFound");
	}			
	
	if(infoObject1.code=="NetStream.Play.Switch")
	{
		//trace("NetStream.Play.Switch");
	}			
	
	if(infoObject1.code=="NetStream.Play.UnpublishNotify")
	{
		//trace("NetStream.Play.UnpublishNotify");
	}			
	
	if(infoObject1.code=="NetStream.Publish.BadName")
	{
		//trace("NetStream.Publish.BadName");
	}			
	
	if(infoObject1.code=="NetStream.Publish.Idle")
	{
		//trace("NetStream.Publish.Idle");
	}			
	
	if(infoObject1.code=="NetStream.Publish.Start")
	{
		//trace("NetStream.Publish.Start");
	}			
	
	if(infoObject1.code=="NetStream.Record.Failed")
	{
		//trace("NetStream.Record.Failed");
	}			
	
	if(infoObject1.code=="NetStream.Record.NoAccess")
	{
		//trace("NetStream.Record.NoAccess");
	}			
	
	if(infoObject1.code=="NetStream.Record.Start")
	{
		//trace("NetStream.Record.Start");
	}			
	
	if(infoObject1.code=="NetStream.Record.Stop")
	{
		//trace("NetStream.Record.Stop");
	}			
	
	if(infoObject1.code=="NetStream.Seek.Failed")
	{
		//trace("NetStream.Seek.Failed");
	}			
	
	if(infoObject1.code=="NetStream.Seek.Notify")
	{
		seeking=true;
		//trace("NetStream.Seek.Notify and toatal_time"+total_time);
		_root.played_tween.stop();
		_root.run_tween.stop();
	}			
	
	if(infoObject1.code=="NetStream.Unpause.Notify")
	{
		//trace("NetStream.Unpause.Notify");
		//test1.htmlText="UNPAUSE";
	}			
	
	if(infoObject1.code=="NetStream.Unpublish.Success")
	{
		//trace("NetStream.Unpublish.Success");
	}						
};

function load_quang_cao_dau(quang_cao_name:String)
{
	txt_ini_loading._visible=true;
	_root.video_region.my_video._visible=false;
	//create clips to hold your content
this.createEmptyMovieClip("video_region1", 0);
//video_region._width=100;
//video_region._height=100;
video_region1.createEmptyMovieClip("bar_mc", 1);
video_region1.createEmptyMovieClip("stroke_mc", 2);
//use drawing methods to create a progress bar
with (video_region1.stroke_mc) {
   lineStyle(0, 0x000000);
   moveTo(0, 0);
   lineTo(100, 0);
   lineTo(100, 10);
   lineTo(0, 10);
   lineTo(0, 0);
}
with (video_region1.bar_mc) {
   beginFill(0xFF0000, 100);
   moveTo(0, 0);
   lineTo(100, 0);
   lineTo(100, 10);
   lineTo(0, 10);
   lineTo(0, 0);
   endFill();
   _xscale = 100;
   _yscale = 100;
}
video_region1._x = 2;
video_region1._y = 2;
// load progress
var mclListener:Object = new Object();
mclListener.onLoadStart = function(target_mc:MovieClip) {
   video_region1.bar_mc._xscale = 0;
};
mclListener.onLoadProgress = function(target_mc:MovieClip, bytesLoaded:Number, bytesTotal:Number) {
   video_region1.bar_mc._xscale = Math.round(bytesLoaded/bytesTotal*100);
};
mclListener.onLoadComplete = function(target_mc:MovieClip) {
   txt_ini_loading._visible=false;	
   video_region1.removeMovieClip();
   bar_mc.removeMovieClip();
   stroke_mc.removeMovieClip();   
   _root.qc_load_complete=true;
   //trace("qc_load_complete="+qc_load_complete);
	if(qc_load_complete==true)
	{
		//trace("bat dau dem thoi gian");
		time_id1 = setInterval(qc_dau_func2,100,stream_ns)
	}
	var qc_count1:Number=0;	

};
mclListener.onLoadInit = function(target_mc:MovieClip) {
   target_mc._height = 300;
   target_mc._xscale=100;
   target_mc._yscale=100;
   target_mc._width = 300;
};
//Create a clip to hold the image.
this.createEmptyMovieClip("image_mc", 0);
var image_mcl:MovieClipLoader = new MovieClipLoader();
image_mcl.addListener(mclListener);
/* Load the image into the clip.
You can change the following URL to a SWF or another image file. */
image_mcl.loadClip(quang_cao_name, image_mc);
}


function load_quang_cao_cuoi(quang_cao_name:String)
{
	//create clips to hold your content
txt_ini_loading._visible=true;		
this.createEmptyMovieClip("video_region2", 1);
video_region2.createEmptyMovieClip("bar_mc", 2);
video_region2.createEmptyMovieClip("stroke_mc", 3);
//use drawing methods to create a progress bar
with (video_region2.stroke_mc) {
   lineStyle(0, 0x000000);
   moveTo(0, 0);
   lineTo(400, 0);
   lineTo(400, 10);
   lineTo(0, 10);
   lineTo(0, 0);
}
with (video_region2.bar_mc) {
   beginFill(0xFF0000, 100);
   moveTo(0, 0);
   lineTo(400, 0);
   lineTo(400, 10);
   lineTo(0, 10);
   lineTo(0, 0);
   endFill();
   _xscale = 0;
}
video_region2._x = 2;
video_region2._y = 2;
// load progress
var mclListener:Object = new Object();
mclListener.onLoadStart = function(target_mc:MovieClip) {
   video_region2.bar_mc._xscale = 0;
};
mclListener.onLoadProgress = function(target_mc:MovieClip, bytesLoaded:Number, bytesTotal:Number) {
   video_region2.bar_mc._xscale = Math.round(bytesLoaded/bytesTotal*100);
};
mclListener.onLoadComplete = function(target_mc:MovieClip) {
	txt_ini_loading._visible=false;			
   video_region2.removeMovieClip();
   bar_mc.removeMovieClip();
   stroke_mc.removeMovieClip();
   _root.qc_load_complete=true;
   //trace("qc_load_complete="+qc_load_complete);
	if(qc_load_complete==true)
	{
		//trace("bat dau dem thoi gian");
		//clearInterval(time_id2);
		time_id3 = setInterval(qc_cuoi_func2,100,end_ad_stream);
	}
	var qcc_count:Number=0;	

};
mclListener.onLoadInit = function(target_mc:MovieClip) {
  target_mc._height = 300;
   target_mc._xscale=100;
   target_mc._yscale=100;
   target_mc._width = 300;

};
//Create a clip to hold the image.
this.createEmptyMovieClip("image_mc", 100);
var image_mcl:MovieClipLoader = new MovieClipLoader();
image_mcl.addListener(mclListener);
/* Load the image into the clip.
You can change the following URL to a SWF or another image file. */
image_mcl.loadClip(quang_cao_name, image_mc);
}
function film_stop()
{
	//trace("STOP ispause:"+_root.is_pause);
		//test1.htmlText="stop";
		load_completed=false;
		_root.slide_in_used = false;		
		if(qc_dau==true||qc_cuoi==true||end_film==true)
			return;
		clearInterval(time_id2);				
		replay_ins._visible=false;
		zoom_in._alpha=50;	
		_root.reset_video();		
		_root.pausing._visible=false;
		_root.played_tween.start();
		_root.played_tween.stop();		
		_root.run_tween.start();
		_root.run_tween.stop();		
		if(_root.mouse_moving==true&&_root.is_pause==false&&_root.button_is_pause==false)
		{
			_root.stream_ns.pause();			
		}		
		if(_root.is_pause==false)	
		{
			_root.stream_ns.pause();
		}
		_root.button_is_pause=false
		_root.mouse_moving=false;
		_root.is_pause==false;
		_root.related_film_enable = true;
		_root.total_time = 0;
		_root.thanh_truot1._x = _root.total_length._x;
		_root.played_ins._width = 0;
		_root.stream_ns.seek(0);
		_root.play_region.play_button_ins._visible = true;
		_root.play_region.pause_button_ins._visible = false;
		_root.time_region.time_playing_str.htmlText="00:00";
		_root.end_film=true;	
		if(_root.qcc_name!=undefined)
		{
			_root.qcc_call();
		}
		else
		{
			_root.video_region.my_video._visible=false;
			_root.related_film_func();		
		}					
		
}